package com.okami.clazz;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author: TanJingyu
 * @create: 2021-03-09 10:19
 **/
public class SimpleDynamicProxy {
    public static void main(String[] args) {
        Father real = new Son();

        Father father = (Father) Proxy.newProxyInstance(
                Father.class.getClassLoader(),
                new Class[]{Father.class},
                new DynamicProxyHandler(real));

        System.out.println(father.show());
    }
}

class DynamicProxyHandler implements InvocationHandler {
    private Object proxied;

    DynamicProxyHandler(Object proxied) {
        this.proxied = proxied;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {

        System.out.println("真正方法之前执行的方法");

        method.invoke(proxied, args);

        return "成了！";
    }
}

interface Father {
    Object show();
}

class Son implements Father {

    @Override
    public Object show() {
        System.out.println("实际对象的show方法。。。。。。");
        return "";
    }
}

